$(function () {
    $('#Commontop').load('../assets/public/html/PublicLoginNotLogIn.html');
    //弹窗
    toastr.options = {
        "closeButton": true,
        "debug": false,
        "positionClass": "toast-bottom-right",
        "onclick": null,
        "showDuration": "300",
        "hideDuration": "1000",
        "timeOut": "5000",
        "extendedTimeOut": "1000",
        "showEasing": "swing",
        "hideEasing": "linear",
        "showMethod": "fadeIn",
        "hideMethod": "fadeOut"
    }


    function errorWindows(text1, text2) {
        toastr.error(`${text2}`, `${text1}`)
    }

    function successWindows(text1, text2) {
        toastr.success(`${text2}`, `${text1}`)
    }

    //防抖
    function debounce(fn, delay) {
        let t = null;
        return function () {
            t && clearTimeout(t); //要是是false则执行左边
            t = setTimeout(function () {
                fn();
            }, delay)
        }
    }

    //切换状态
    function changeLogin() {
        let status = true;
        $('.codeReg,.passReg').on('click', '.change_login', function () {
            if (status == 1) {
                $('#loginPhoneCon').hide()
                $('#loginPassCon').show();
            } else {
                $('#loginPassCon').hide();
                $('#loginPhoneCon').show()
            }
            status = !status;
        })
    }

    changeLogin()

    $('.code_btn').click(debounce(code_sumbit, 500))
    $('.pass_btn').click(debounce(pass_sumbit, 500))


    let vaildatorReCode = $('#loginPhoneCon').validate({
        debug: true,
        rules: {
            rePhoneCode: {
                required: true,
                minlength: 11,
                maxlength: 11,
                digits: true
            },
            reMessageCode: {
                required: true,
                minlength: 4,
                maxlength: 4,
                digits: true
            }
        },
        messages: {
            rePhoneCode: {
                required: '手机号不能为空',
                minlength: '请输入11位手机号',
                maxlength: '请输入11位手机号',
                digits: '请输入数字',
            }, reMessageCode: {
                required: '验证码不能为空',
                minlength: '验证长度为4位',
                maxlength: '验证长度为4位',
                digits: '请输入数字'
            }
        }
    })

    let vaildatorRepass = $('#loginPassCon').validate({
        debug: true,
        rules: {
            rePhoneCode2: {
                required: true,
                minlength: 11,
                maxlength: 11,
                digits: true
            },
            repass: {
                required: true,
                rangelength: [6, 10]
            },
            reConfirmPass: {
                required: true,
                rangelength: [6, 10],
                equalTo: '#repass'
            }
        },
        messages: {
            rePhoneCode2: {
                required: '手机号不能为空',
                minlength: '请输入11位手机号',
                maxlength: '请输入11位手机号',
                digits: '请输入数字',
            },
            repass: {
                required: '密码不能为空',
                rangelength: '密码长度位6-10'
            },
            reConfirmPass: {
                required: '密码不能为空',
                rangelength: '密码长度位6-10',
                equalTo: '密码不一致'
            }
        }
    })

    //密码注册
    function pass_sumbit() {
        let isChecked = $('.check').prop('checked')
        if (isChecked) {
            if (vaildatorRepass.form()) {
                let passRegister = Interceptor('/passRegister', 'post', {
                    'phone': $('#phonePass').val(),
                    'password': $('#repass').val(),
                    'rePassword': $('#reConfirmPass').val(),
                })
                const {code, message} = passRegister
                if (code != 304) {
                    errorWindows(message, '')
                } else {
                    successWindows(message, '4秒后跳转登录页');
                    successRegister()
                }
            }
        } else {
            errorWindows('请勾选协议', '')
        }
    }

    //验证码注册
    function code_sumbit() {
        if ($('.check').get(0).checked) {
            if (vaildatorReCode.form()) {
                let phoneRegister = Interceptor('/phoneRegister', 'post',
                    {
                        'phone': $('#phoneCode').val(),
                        'code': $('#messageCode').val()
                    }
                )
                const {code, message} = phoneRegister;
                if (code != 304) {
                    errorWindows(message, '')
                } else {
                    successWindows(message, '4秒后跳转登录页');
                    successRegister();
                }
            }
        } else {
            errorWindows('请勾选协议', '')
        }
    }


    $('.send_code a').click(sendCode)

    let myreg = /^1[3456789]\d{9}$/;

    let sendTime = 60
    //限制短信发送时间
    limitedTime = function (dom) {
        if (sendTime == 0) {
            dom.css({'pointer-events':'auto'})
            dom.prop('disabled', false)
            dom.text('获取验证码');
            sendTime = 60
            return
        } else {
            sendTime--
            dom.css({'pointer-events':'none'})
            dom.prop('disabled', true)
            dom.text(`${sendTime}秒后重新发送`)
        }
        setTimeout(() => {
            limitedTime(dom)
        }, 1000)
    }

    //发送短信
    function sendCode() {
        let flag = false;
        if ($('#phoneCode').val() == '') {
            $('.errorMsg').show().html('手机号不能为空')
            setTimeout(() => {
                $('.errorMsg').fadeOut()
            }, 7000)


        } else if (!myreg.test($('#phoneCode').val())) {
            $('.errorMsg').show().html('手机号格式不正确')

            setTimeout(() => {
                $('.errorMsg').fadeOut()
            }, 7000)
        } else {
            flag = true;
        }

        if (flag) {
            const send = Interceptor('/send', 'post', {
                'phone': $('#phoneCode').val(),
            })
            const {code, message} = send
            if (code != 666) {
                errorWindows('发送短信', '')
            } else {
                limitedTime($(this))

                successWindows('发送短信', message)
            }

        }



    }

    //成功注册的事件
    function successRegister() {
        $('.circle').attr('class', 'circle circleAnimation')
        $('.tick').attr('class', 'tick tickAnimation')
        $('.successText').addClass('succesTextAnimation')
        $('.login_sq').hide();
        $('.agree_box').hide();
        $('.succes_register').show();
        setTimeout(() => {
            window.location.replace('Login.html');
        }, 4000)
    }
})